package cn.doyens.controller;

import cn.doyens.bean.Account;
import cn.doyens.dao.AccountMapper;
import cn.doyens.service.AccountService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/acc")
public class AccountController {
    @Autowired
    AccountService acc;

    @RequestMapping("/h1")
    String h1(int ys,int yr) throws IOException {
     acc.findAll();
        return "jin";
    }
    @RequestMapping("/showDoctor.do")
    public String showDoctor(@RequestParam(required= true, defaultValue="1") Integer page, HttpServletRequest request, Model model, String keywords) {
// @RequestParam(required= true, defaultValue="1")设置page的默认值是1
        PageInfo<Account> p = acc.findAl(page);

        model.addAttribute("page",p);
        List<Account> list = p.getList();
        model.addAttribute("list",list);
        return "jin";
    }
    @RequestMapping("/tr")
    public String zu(int id1,int id2,int money){
        acc.zu(id1, id2, money);
        return "jinqu";
    }
    @RequestMapping("/file")
    public String file(MultipartFile upload){

        String originalFilename = upload.getOriginalFilename();
        String newFilename= UUID.randomUUID()+"_"+originalFilename;
        File file = new File("J:\\tcwj\\" + newFilename);
        try {
            upload.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        return "jinqu";
    }
    @RequestMapping("/pper")
    public String pper(@Param("name")String name,@Param("sex")String sex,@Param("date")String date,@Param("bumen")String bumen){
        System.out.println(name);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = simpleDateFormat.parse(date);
            System.out.println(parse);
            acc.insert(name, sex, parse, bumen);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "jinqu";
    }

    @RequestMapping("/redis")
    public ModelAndView redis(){
        ModelAndView modelAndView = new ModelAndView();
        List<Account> redis = acc.redis();
        modelAndView.addObject("re",redis);
        modelAndView.setViewName("ququ");
        return modelAndView;
    }
}
